from wxpyview.ui.component.property.Property import *

class FontProperty(Property):
    FONTFAMILY_MAP = {'default': wx.FONTFAMILY_DEFAULT,
                      'decorative': wx.FONTFAMILY_DECORATIVE,
                      'max': wx.FONTFAMILY_MAX,
                      'modern': wx.FONTFAMILY_MODERN,
                      'roman': wx.FONTFAMILY_ROMAN,
                      'script': wx.FONTFAMILY_SCRIPT,
                      'swiss': wx.FONTFAMILY_SWISS,
                      'teletype': wx.FONTFAMILY_TELETYPE}
    
    FONTSTYLE_MAP = {'italic': wx.FONTSTYLE_ITALIC,
                     'normal': wx.FONTSTYLE_NORMAL,
                     'slant': wx.FONTSTYLE_SLANT}   

    FONTWEIGHT_MAP = {'bold': wx.FONTWEIGHT_BOLD,
                      'light': wx.FONTWEIGHT_LIGHT,
                      'normal': wx.FONTWEIGHT_NORMAL}

    @classmethod    
    def set(cls, meta_widget, real_widget):
        dom = meta_widget.getDOM()
        
        default_font = real_widget.GetFont()
        
        params = {}
        params['pointSize'] = FontProperty._GetFontSize(dom, default_font)
        params['family'] = FontProperty._GetFamily(dom, default_font)
        params['style'] = FontProperty._GetStyle(dom, default_font)
        params['weight'] = FontProperty._GetWeight(dom, default_font)
        params['underline'] = FontProperty._GetUnderlined(dom, default_font)
        params['face'] = FontProperty._GetFaceName(dom, default_font)
        params['encoding'] = FontProperty._GetEncoding(dom, default_font)
        
        font = wx.Font(**params)
               
        real_widget.SetFont(font)

    @classmethod
    def _GetFontSize(cls, dom, default_font):
        attr = dom.getAttribute('fontSize')
        
        if attr == '': return default_font.GetPointSize()
        else: return int(attr)
    
    @classmethod
    def _GetFamily(cls, dom, default_font):       
        attr = dom.getAttribute('fontFamily').lower()
        return FontProperty.FONTFAMILY_MAP.get(attr, default_font.GetFamily())

    @classmethod    
    def _GetStyle(cls, dom, default_font):
        attr = dom.getAttribute('fontStyle').lower()
        return FontProperty.FONTSTYLE_MAP.get(attr, default_font.GetStyle())

    @classmethod    
    def _GetWeight(cls, dom, default_font):
        attr = dom.getAttribute('fontWeight').lower()
        return FontProperty.FONTWEIGHT_MAP.get(attr, default_font.GetWeight())

    @classmethod    
    def _GetUnderlined(cls, dom, default_font):
        attr = dom.getAttribute('fontUnderline').lower()
        
        if attr == 'true': return True
        else: return default_font.GetUnderlined()
    
    @classmethod    
    def _GetFaceName(cls, dom, default_font):
        attr = dom.getAttribute('font')
        return attr

    @classmethod    
    def _GetEncoding(cls, dom, default_font):
        # TODO
        return default_font.GetDefaultEncoding()